package com.mcp.converter.domain.rule.conversion.datatype;

import com.mcp.converter.domain.rule.conversion.ConversionRule;
import com.mcp.converter.shared.enums.IncompatibilityType;
import com.mcp.converter.shared.enums.SQLElementType;
import com.mcp.converter.domain.converter.ConversionSuggestion;
import com.mcp.converter.shared.response.sql.SQLElement;

/**
 * LONG RAW数据类型转换规则
 * 将LONG RAW数据类型转换为BLOB数据类型
 * @author zengxiong
 */
public class LongRawToBlobConversionRule implements ConversionRule {

    private static final String RULE_ID = "CV_DT002";
    private static final String RULE_NAME = "LONG RAW转BLOB";
    private static final String RULE_DESCRIPTION = "将LONG RAW数据类型转换为BLOB数据类型。";

    private ConversionSuggestion conversionSuggestion;

    public LongRawToBlobConversionRule() {
        this.conversionSuggestion = new ConversionSuggestion();
        this.conversionSuggestion.setSuggestion("将LONG RAW数据类型替换为BLOB数据类型。");
        this.conversionSuggestion.setExample("CREATE TABLE example (raw_data LONG RAW) -> CREATE TABLE example (raw_data BLOB)");
        this.conversionSuggestion.setAutoConvertible(true);
        this.conversionSuggestion.setComplexity(ConversionSuggestion.Complexity.SIMPLE);
    }

    @Override
    public String getId() {
        return RULE_ID;
    }

    @Override
    public String getName() {
        return RULE_NAME;
    }

    @Override
    public String getDescription() {
        return RULE_DESCRIPTION;
    }

    @Override
    public IncompatibilityType getIncompatibilityType() {
        return IncompatibilityType.DATA_TYPE;
    }

    @Override
    public ConversionSuggestion getConversionSuggestion() {
        return conversionSuggestion;
    }

    @Override
    public boolean matches(Object target) {
        if (target instanceof SQLElement) {
            SQLElement element = (SQLElement) target;
            if (element.getType() == SQLElementType.DATA_TYPE) {
                String dataTypeName = element.getName() != null ? element.getName().toUpperCase() : (element.getValue() != null ? element.getValue().toUpperCase() : "");
                return "LONG RAW".equals(dataTypeName);
            }
        }
        return false;
    }

    @Override
    public String convert(SQLElement element, ConversionContext context) {
        String original = element.getValue() != null ? element.getValue() : "LONG RAW";
        String converted = "BLOB";

        if (context != null) {
            context.addConversionDetail(original, converted, "LONG RAW数据类型在OceanBase中不支持，已转换为BLOB。");
        }
        
        return converted;
    }
}
